﻿namespace com.crownpack.Extensions.SharePointExtensions
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    public static partial class SharePointExtensions
    {
        #region Methods

        public static SPSite GetSite( this SPFeatureReceiverProperties properties )
        {
            SPSite spSite = null;
            SPWeb spWeb = properties.GetWeb( );
            if ( spWeb == null )
            {
                if ( properties.Feature != null )
                    spSite = properties.Feature.Parent as SPSite;
            }
            else
                spSite = spWeb.Site;
            return spSite;
        }

        public static SPWeb GetWeb( this SPFeatureReceiverProperties properties )
        {
            SPWeb spWeb = null;
            if ( properties.Feature != null )
            {
                spWeb = properties.Feature.Parent as SPWeb;
                if ( spWeb == null )
                {
                    SPSite spSite = properties.Feature.Parent as SPSite;
                    if ( spSite != null )
                        spWeb = spSite.OpenWeb( );
                }
            }
            return spWeb;
        }

        public static SPWebApplication GetWebApplication( this SPFeatureReceiverProperties properties )
        {
            return properties.GetSite( ).WebApplication;
        }

        public static string GetWebUrl( this SPFeatureReceiverProperties properties )
        {
            using ( SPWeb spWeb = properties.GetWeb( ) )
            {
                return spWeb.Url;
            }
        }

        #endregion Methods
    }
}